package MyGO.controller;

import MyGO.property.Medicine;
import MyGO.service.IMedicineService;
import MyGO.service.impl.MedicineServiceImpl;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class Yaowu_genggai {

    @FXML
    private TextField changjia;

    @FXML
    private TextField id;

    @FXML
    private TextField name;

    @FXML
    private TextField price;

    @FXML
    private TextField size;

    @FXML
    private Button sure;

    public void initialize()
    {
        //修改药物时先显示当前药物信息
        if (Yaoku_guanli.getMedicine() != null){
            changjia.setText(Yaoku_guanli.getMedicine().getManufacturer());
            id.setText(Yaoku_guanli.getMedicine().getId());
            name.setText(Yaoku_guanli.getMedicine().getName());
            price.setText(Yaoku_guanli.getMedicine().getPriceStr());
            size.setText(Yaoku_guanli.getMedicine().getFormat());
        }
        sure.setOnAction(event -> {
            if (Yaoku_guanli.getMedicine() != null){   //修改药物
                setMedicine(Yaoku_guanli.getMedicine());
            }else{
                Medicine temp = new Medicine();      //添加新药物
                setMedicine(temp);
                IMedicineService m = new MedicineServiceImpl();
                m.addMedicine(temp);
            }
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setHeaderText("药物更改成功!");
            alert.showAndWait();
            Yaoku_guanli.getNewsStage().close();
        });
    }
    private void setMedicine(Medicine medicine){
        //如果有一项未输入
        if (changjia.getText().isEmpty() || name.getText().isEmpty() || size.getText().isEmpty() || price.getText().isEmpty() || id.getText().isEmpty()){
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setHeaderText("请输入完整的信息!");
            alert.showAndWait();
        }else {
            medicine.setManufacturer(changjia.getText());
            medicine.setName(name.getText());
            medicine.setFormat(size.getText());
            medicine.setPrice(Double.parseDouble(price.getText()));
            medicine.setId(id.getText());
        }
    }

}
